package com.cucn.ingmall.member.controller;

import cn.hutool.core.util.ObjectUtil;
import com.cucn.common.exception.BizCodeEnume;
import com.cucn.common.utils.PageUtils;
import com.cucn.common.utils.R;
import com.cucn.ingmall.member.entity.MemberEntity;
import com.cucn.ingmall.member.exception.PhoneExistExcetion;
import com.cucn.ingmall.member.exception.UserNameExistException;
import com.cucn.ingmall.member.feign.CouponFeignService;
import com.cucn.ingmall.member.service.MemberService;
import com.cucn.ingmall.member.vo.MemberLoginVO;
import com.cucn.ingmall.member.vo.MemberRegistVO;
import com.cucn.ingmall.member.vo.SocialUser;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.Map;
import java.util.Objects;


/**
 * 会员
 *
 * @author kouchaojie
 * @email kou1688@foxmail.com
 * @date 2021-11-17 18:26:50
 */
@Slf4j
@RestController
@RequestMapping("member/member")
public class MemberController {
    @Autowired
    private MemberService memberService;

    @Autowired
    CouponFeignService couponFeignService;

    @RequestMapping("/coupons")
    public R test() {
        MemberEntity memberEntity = new MemberEntity();
        memberEntity.setNickname("张三");

        R membercoupons = couponFeignService.membercoupons();
        return Objects.requireNonNull(R.ok().put("member", memberEntity)).put("coupons", membercoupons.get("coupons"));
    }

    /**
     * 用户注册方法
     *
     * @param registVO 认证服务传过来的用户注册信息
     * @return
     */
    @PostMapping("/regist")
    public R regist(@RequestBody MemberRegistVO registVO) {
        try {
            log.info("用户注册信息:{}", registVO);
            memberService.regist(registVO);
        } catch (PhoneExistExcetion e) {
            log.error("手机号已经被注册!", e);
            return R.error(BizCodeEnume.PHONE_EXIST_EXCEPTION.getCode(), BizCodeEnume.PHONE_EXIST_EXCEPTION.getMsg());
        } catch (UserNameExistException e) {
            log.error("用户名已经被注册!", e);
            return R.error(BizCodeEnume.USER_EXIST_EXCEPTION.getCode(), BizCodeEnume.USER_EXIST_EXCEPTION.getMsg());
        }

        return R.ok();
    }

    /**
     * 用户登录方法
     *
     * @param loginVO 登录vo
     * @return
     */
    @PostMapping("/login")
    public R login(@RequestBody MemberLoginVO loginVO) {
        log.info("用户:{}-->进行帐号密码登录...", loginVO.getLoginacct());
        MemberEntity member = memberService.login(loginVO);
        if (ObjectUtil.isNotEmpty(member)) {
            //KouTODO:2022/1/11 10:36 Kou 登录成功处理
            return R.ok().setData(member);
        } else {
            return R.error(BizCodeEnume.LOGINACCT_PASSWORD_INVAILD_EXCEPTION.getCode(),
                    BizCodeEnume.LOGINACCT_PASSWORD_INVAILD_EXCEPTION.getMsg());
        }
    }

    /**
     * 用户社交登录方法
     *
     * @param socialUser 社交用户回调信息(token)
     * @return
     */
    @PostMapping("/oauth2/login")
    public R login(@RequestBody SocialUser socialUser) throws Exception {
        log.info("社交登录进行....{}", socialUser);
        MemberEntity member = memberService.authLogin(socialUser);
        if (ObjectUtil.isNotEmpty(member)) {
            //KouTODO:2022/1/11 10:36 Kou 登录成功处理
            return R.ok().setData(member);
        } else {
            return R.error(BizCodeEnume.LOGINACCT_PASSWORD_INVAILD_EXCEPTION.getCode(),
                    BizCodeEnume.LOGINACCT_PASSWORD_INVAILD_EXCEPTION.getMsg());
        }
    }

    /**
     * 列表
     */
    @RequestMapping("/list")
    //@RequiresPermissions("member:member:list")
    public R list(@RequestParam Map<String, Object> params) {
        PageUtils page = memberService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    //@RequiresPermissions("member:member:info")
    public R info(@PathVariable("id") Long id) {
        MemberEntity member = memberService.getById(id);

        return R.ok().put("member", member);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    //@RequiresPermissions("member:member:save")
    public R save(@RequestBody MemberEntity member) {
        memberService.save(member);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    //@RequiresPermissions("member:member:update")
    public R update(@RequestBody MemberEntity member) {
        memberService.updateById(member);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    //@RequiresPermissions("member:member:delete")
    public R delete(@RequestBody Long[] ids) {
        memberService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

}
